package com.huahua.graduateschoolspringboot.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import com.huahua.graduateschoolspringboot.entity.R;
import com.huahua.graduateschoolspringboot.entity.po.CityBusDevice;
import com.huahua.graduateschoolspringboot.entity.vo.CityBusDeviceVo;
import com.huahua.graduateschoolspringboot.service.CityBusDeviceService;
import com.huahua.graduateschoolspringboot.util.RecognizeUtil;
import com.huahua.graduateschoolspringboot.util.SmsUtil;
import com.sun.xml.internal.bind.v2.schemagen.episode.Klass;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author LoveHuaHua
 * @date 2023年05月08日 16:46
 * @description believe in yourself
 */
@RestController
@RequestMapping("/bus")
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class BusController {

    private final CityBusDeviceService cityBusDeviceService;

    private LocalDateTime preTime;

    @GetMapping("/getBusStatus")
    public Object getBusStatus() throws IOException, TencentCloudSDKException {
        List<CityBusDevice> list = cityBusDeviceService.list();
        List<CityBusDeviceVo> cityBusDeviceVos = BeanUtil.copyToList(list, CityBusDeviceVo.class);
        for (CityBusDeviceVo cityBusDeviceVo : cityBusDeviceVos) {
/*            //WGS84 坐标转为 墨卡托投影
            CoordinateUtil.Coordinate coordinate = CoordinateUtil.wgs84ToMercator(cityBusDeviceVo.getLongitude(), cityBusDeviceVo.getLatitude());
            CoordinateUtil.Coordinate initCoordinate = CoordinateUtil.wgs84ToMercator(2577406.4340769146, 1.2033502295275511E7);

            cityBusDeviceVo.setX(coordinate.getLng() -initCoordinate.getLng() );
            cityBusDeviceVo.setZ(coordinate.getLat() - initCoordinate.getLat());*/
            Integer personCount = 0;
//            try {
//                personCount = RecognizeUtil.obtainPersonCount("http://" + cityBusDeviceVo.getIp() + "/jpg");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            Integer personCount = RecognizeUtil.obtainPersonCount("https://gss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/4afbfbedab64034fd7c7b1e6afc379310b551dc3.jpg");
            //人数紧张
            if (personCount >= cityBusDeviceVo.getMaxCount()) {
                if (preTime.isBefore(LocalDateTime.now().minusMinutes(5))) {
                    //报警时间超长
                    SmsUtil.sendSms();
                    preTime = LocalDateTime.now();
                }

            }
            cityBusDeviceVo.setPersonCount(personCount);
        }
        //返回前端key为车名的数据 便于前端取
        return R.success(CollStreamUtil.toIdentityMap(cityBusDeviceVos, CityBusDevice::getName));
    }


    /**
     * 请求获取人员列表
     *
     * @return
     */
    @GetMapping("/requestPersonCount")
    public Object requestPersonCount() throws IOException {
        CityBusDevice byId = cityBusDeviceService.getById(1);
        //通过ip获取截图请求api获取人数
        Integer personCount = RecognizeUtil.obtainPersonCount("http://" + byId.getIp() + "/jpg");
        System.out.println(personCount);
        return R.success(personCount);
    }

    /**
     * 更新最大数量
     *
     * @param cityBusDevice 城市客车设备
     * @return {@link Object}
     */
    @PostMapping("/updateMaxCount")
    public Object updateMaxCount(@RequestBody CityBusDevice cityBusDevice) {
        cityBusDeviceService.lambdaUpdate().set(CityBusDevice::getMaxCount, cityBusDevice.getMaxCount()).update();
        return R.success();
    }
}
